//
//  ViewController.m
//  CodeForANF2
//
//  Created by relax on 2017/11/13.
//  Copyright © 2017年 relax. All rights reserved.
//

#import "ViewController.h"
#import "RLNetworkCache.h"
#import "RLNetworkHelper.h"

@interface ViewController ()

@property (nonatomic,copy) void(^block)(NSString *);
@property (weak, nonatomic) IBOutlet UIProgressView *downloadProgress;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.block = ^(NSString *thing) {
        NSLog(@"%@",thing);
    };


    [self monitoringNetStatus];
}

#pragma mark - GET 请求demo
- (IBAction)getRequestClick:(id)sender {
    // 如果服务器需要JSON数据类型的格式，需要配置一下。可以在 AppDelegate 里全局配置。
    // [RLNetworkHelper setHttpRequesetSerializer:RLRequestSerializerJSON];

    // 1. 请求的链接地址字符串
    NSString *getRequestURLString = @"http://localhost/php/login/login.php";
    NSDictionary *getParams = @{
                                @"username" : @"zhangsan",
                                @"password" : @"zhang"
                                };
    NSURLSessionDataTask *getTask = [RLNetworkHelper GET:getRequestURLString parameters:getParams success:^(id responseObject) {
        NSLog(@"GET 请求成功 %@",responseObject);
    } failure:^(NSError *error) {
        NSLog(@"GET 请求失败 %@",error);
    }];

    [getTask resume];
}

- (IBAction)postRequestDemo:(id)sender {
    // 1. 请求的URL链接地址
    NSString *postURLString = @"http://localhost/php/login/login.php";

    // 2. 请求参数
    NSDictionary *postParams = @{
                                 @"username" : @"zhangsan",
                                 @"password" : @"zhang"
                                 };

    // 3. 发送 POST 请求
    NSURLSessionDataTask *postTask = [RLNetworkHelper POST:postURLString parameters:postParams success:^(id responseObject) {
        NSLog(@"POST 请求成功 %@",responseObject);
    } failure:^(NSError *error) {
        NSLog(@"POST 请求失败 %@",error);
    }];

    [postTask resume];

}

#pragma mark - 文件上传demo
- (IBAction)uploadClick:(id)sender {
    // 1. 要上传的文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1.jpg" ofType:nil];
    // 2. 上传的地址
    NSString *uploadURL = @"http://localhost/php/upload/upload.php";

    NSURLSessionUploadTask *uploadTask = [RLNetworkHelper uploadFileWhitURL:uploadURL parameters:nil name:@"userfile" filePath:filePath progress:nil success:^(id responseObject) {
        NSLog(@"文件上传成功! %@", responseObject);
    } failure:^(NSError *error) {
        NSLog(@"文件上传失败! %@", error);
    }];

    [uploadTask resume];
}


#pragma mark - 文件下载demo
- (IBAction)downloadClick:(UIButton *)sender {
    sender.enabled = NO;
    [sender setTitle:@"downloading" forState:UIControlStateDisabled];
    // 下载文件链接
    NSString *downloadFileURLString = @"http://127.0.0.1/11.zip";
    // NSString *fileDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:downloadFileURLString.lastPathComponent];
    NSURLSessionDownloadTask *downloadTask = [RLNetworkHelper downloadWithURL:downloadFileURLString fileDir:nil progess:^(NSProgress *progrss) {
        self.downloadProgress.progress = (float)progrss.completedUnitCount / progrss.totalUnitCount;
    } sucess:^(id responseObject) {
        NSLog(@"%@ filePath = %@",@"文件保存成功!",responseObject);
        sender.enabled = YES;
        [sender setTitle:@"下载完成" forState:UIControlStateNormal];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [sender setTitle:@"下载失败" forState:UIControlStateNormal];
    }];

    [downloadTask resume];
}

#pragma mark - 多图片上传
- (IBAction)mutilImageUpload:(id)sender {
    // 1. 需要上传的文件图片组
    NSArray *images = @[[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"]];

    // 2. 上传的地址
    NSString *mutilUploadURLString = @"http://localhost/php/upload/upload-m.php";

    // 3. 上传的服务器端字段
    NSString *serverName = @"userfile[]";

    // 4. 开始上传文件
    NSURLSessionUploadTask *uploadTask = [RLNetworkHelper uploadImagesWithURL:mutilUploadURLString parameters:nil name:serverName images:images fileNames:nil imageScale:0.5 imageType:@"jpg" progess:nil success:^(id responseObject) {
        NSLog(@"%@",@"上传成功!");
    } faliure:^(NSError *error) {
        NSLog(@"%@",error);
    }];

    [uploadTask resume];
}

#pragma mark - 监听网络状态
- (void)monitoringNetStatus {
    // 监听网络状态。
    [RLNetworkHelper networkStatusWithBlock:^(RLNetworkStatusType statusType){
        switch (statusType) {
            case RLNetworkStatusTypeUnknow:
            case RLNetworkStatusTypeUNReachable:
                NSLog(@"%@",@"未知状态 or 无网络");
                // 从缓存读取数据，刷新表格
                break;

            case RLNetworkStatusTypeReachableViaWiFi:
                NSLog(@"%@",@"WiFi");
                break;

            case RLNetworkStatusTypeReachableViaWWAN:
                NSLog(@"%@",@"手机流量");
                break;

            default:
                break;
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // [RLNetworkCache new];
    self.block ? self.block(@"和尼玛") : nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
